﻿using System;
using System.Configuration;
using System.Xml;

namespace MvcForum.Net.Configuration
{
    public class ForumConfigSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            return new ForumConfiguration(section);
        }

    }


    public sealed class ForumConfiguration
    {

        private const string CONFIG_SECTION = "MvcForum.Net";

        private readonly XmlNode _section;

        static ForumConfiguration settings;


        public ForumConfiguration(XmlNode node)
        {
            _section = node;
        }

        public string this[string key]
        {
            get
            {
                XmlNode node = _section.SelectSingleNode(key);
                if (node != null)
                    return node.InnerText;
                return null;
            }
        }



        // Create singleton object of the settings
        public static ForumConfiguration Instance
        {
            get
            {

                // get the config settings


                settings = (ForumConfiguration)ConfigurationManager.GetSection(CONFIG_SECTION);
                if (settings == null)
                {
                    if (System.Web.HttpContext.Current == null)
                        throw new ApplicationException("The main forum control is not design-time compatible due to it's complexity. Please load the individual page controls to modify the forum.");
                    throw new ApplicationException("Failed to get configuration from Web.config.");
                }



                return settings;
            }
        }



        #region Forum Settings
        [ConfigurationProperty("TimeZone", IsRequired = false)]
        public float TimeZoneOffset
        {
            get
            {
                return (settings["TimeZone"] == null) ? 7 : float.Parse(settings["TimeZone"]);
            }

        }


        public string DateTimeFormat
        {
            get
            {
                return settings["DateTimeFormat"] ?? "MM/d/yyyy hh:mm tt";
            }

        }



        [ConfigurationProperty("IsUrlPlainText", IsRequired = false)]
        public bool IsUrlPlainText
        {
            get
            {
                return (settings["IsUrlPlainText"] == null) ? true : bool.Parse(settings["IsUrlPlainText"]);
            }

        }



        [ConfigurationProperty("EncryptPassword", IsRequired = false)]
        public string EncryptPassword
        {

            get
            {
                return settings["EncryptPassword"] ?? "MvcForum.Net";
            }
        }


        [ConfigurationProperty("MetaTags", IsRequired = false)]
        public string MetaTags
        {

            get
            {
                return settings["MetaTags"];
            }
        }


        [ConfigurationProperty("GoogleAnalytics", IsRequired = false)]
        public string GoogleAnalytics
        {

            get
            {
                return settings["GoogleAnalytics"];
            }
        }

        [ConfigurationProperty("ToolBar", IsRequired = false)]
        public string ToolBar
        {

            get
            {
                return settings["ToolBar"];
            }
        }



        [ConfigurationProperty("CacheEnable", IsRequired = false)]
        public bool CacheEnable
        {
            get
            {
                return (settings["CacheEnable"] == null) ? true : bool.Parse(settings["CacheEnable"]);
            }

        }


        [ConfigurationProperty("RegisterEmailActiveRequired", IsRequired = false)]
        public bool RegisterEmailActiveRequired
        {
            get
            {
                return (settings["RegisterEmailActiveRequired"] == null) ? true : bool.Parse(settings["RegisterEmailActiveRequired"]);
            }

        }


        public int SecondsInCache
        {
            get
            {


                return (settings["SecondsInCache"] == null) ? 0 : int.Parse(settings["SecondsInCache"]);
            }

        }


        public int MinutePageRefresh
        {
            get
            {

                return (settings["MinutePageRefresh"] == null) ? 20 : int.Parse(settings["MinutePageRefresh"]);
            }

        }


        [ConfigurationProperty("PastMinutes", IsRequired = false)]
        public int PastMinutes
        {
            get
            {

                return (settings["PastMinutes"] == null) ? 20 : int.Parse(settings["PastMinutes"]);
            }

        }


        public int PageSize
        {
            get
            {
                return (settings["PageSize"] == null) ? 25 : int.Parse(settings["PageSize"]);
            }

        }


        public string DefaultLanguage
        {
            get
            {
                return settings["DefaultLanguage"] ?? "en";
            }


        }

        public string Extension
        {
            get
            {
                return settings["Extension"] ?? ".aspx";
            }


        }


        public string AbsoluteUrl
        {
            get
            {
                return settings["AbsoluteUrl"];
            }
        }

        //public string ApplicationUrl
        //{
        //    get
        //    {
        //        return settings["ApplicationUrl"] ?? "/forum/";
        //    }

        //}


        public string DataBackupFolder
        {
            get
            {
                return settings["DataBackupFolder"] ?? "/dbbak/";
            }

        }


        public string SkinName
        {
            get
            {
                return settings["SkinName"] ?? "default";
            }

        }


        public string ForumPrefix
        {
            get
            {
                return settings["ForumPrefix"] ?? "Nasa8xForum_";
            }

        }


        public string DisallowedUserNames
        {
            get
            {
                return settings["DisallowedUserNames"] ?? string.Empty;
            }

        }

        public string AllowEmails
        {
            get
            {
                return settings["AllowEmails"] ?? string.Empty;
            }

        }


         


        


        /// <summary>
        /// The name of the forum.
        /// </summary>
        public string ForumName
        {
            get { return settings["ForumName"] ?? "NASA8x.Forum"; }
        }



        public string Keywords
        {
            get { return settings["Keywords"] ?? ForumName; }
        }

        public string Description
        {
            get { return settings["Description"] ?? ForumName; }
        }

        //public string CopyRight
        //{
        //    get { return settings["CopyRight"] ?? "Copyright © 2009, WWW.XOMNET.ORG"; }
        //}

        public string Version
        {
            get { return settings["ForumVersion"] ?? "2009.1.1.20"; }
        }


        /// <summary>
        /// The from address when sending emails to users.
        /// </summary>
        public string ForumEmail
        {
            get { return settings["ForumEmail"] ?? "nasavietnam@gmail.com"; }

        }



        public bool SendMailToAdminWhenNewPost
        {
            get { return (settings["SendMailToAdminWhenNewPost"] == null) ? false : bool.Parse(settings["SendMailToAdminWhenNewPost"]); }

        }


        /// <summary>
        /// Number of posts to show per page.
        /// </summary>
        [ConfigurationProperty("PostsPerPage", IsRequired = false)]
        public int PostsPerPage
        {
            get { return (settings["PostsPerPage"] == null) ? 25 : int.Parse(settings["PostsPerPage"]); }


        }

        /// <summary>
        /// Number of topics to show per page.
        /// </summary>
        [ConfigurationProperty("TopicsPerPage", IsRequired = false)]
        public int TopicsPerPage
        {
            get { return (settings["TopicsPerPage"] == null) ? 25 : int.Parse(settings["TopicsPerPage"]); }

        }
        /// <summary>
        /// Number of words show in title
        /// </summary>

        public int TopicShowWordsLength
        {
            get { return (settings["TopicShowWordsLength"] == null) ? 9 : int.Parse(settings["TopicShowWordsLength"]); }

        }


        /// <summary>
        /// Number of words show in title
        /// </summary>

        public int PostShowWordsLength
        {
            get { return (settings["PostShowWordsLength"] == null) ? 5 : int.Parse(settings["PostShowWordsLength"]); }

        }



        /// <summary>
        /// Number of Minutes until posts are locked and not possible to edit or delete. Set to 0 for no limit.
        /// </summary>

        public int DaysLockPosts
        {
            get { return (settings["DaysLockPosts"] == null) ? 0 : int.Parse(settings["DaysLockPosts"]); }


        }

        public int HotReplyThreshold
        {
            get { return (settings["HotReplyThreshold"] == null) ? 10 : int.Parse(settings["HotReplyThreshold"]); }


        }


        public int NewReplyDaysAgo
        {
            get { return (settings["NewReplyDaysAgo"] == null) ? 1 : int.Parse(settings["NewReplyDaysAgo"]); }


        }



        [ConfigurationProperty("UploadTempDir", IsRequired = false)]
        public string UploadTempDir
        {

            get
            {
                return settings["UploadTempDir"] ?? "Upload/Temp/";
            }


        }

        /// <summary>
        /// Maximum size for users upload in kb(Does not apply to admins or mods)
        /// </summary>
        public int MaxFileSize
        {
            get { return (settings["MaxFileSize"] == null) ? 200 : int.Parse(settings["MaxFileSize"]); }
        }

        /// <summary>
        /// Number of seconds before another post can be entered. (Does not apply to admins or mods.)
        /// </summary>
        [ConfigurationProperty("PostFloodDelay", IsRequired = false)]
        public int PostFloodDelay
        {
            get { return (settings["PostFloodDelay"] == null) ? 30 : int.Parse(settings["PostFloodDelay"]); }
        }

        #endregion

        #region Search Settings

        

        public bool AllowGuestSearch
        {
            get { return (settings["AllowGuestSearch"] == null) ? true : bool.Parse(settings["AllowGuestSearch"]); }

        }

        public int MaxSearchResults
        {
            get { return (settings["MaxSearchResults"] == null) ? 500 : int.Parse(settings["MaxSearchResults"]); }

        }


        /// <summary>
        /// Number of seconds before another search can be entered. (Does not apply to admins or mods.)
        /// </summary>
        [ConfigurationProperty("SearchFloodDelay", IsRequired = false)]
        public int SearchFloodDelay
        {
            get { return (settings["SearchFloodDelay"] == null) ? 0 : int.Parse(settings["SearchFloodDelay"]); }
        }



        public string SearchResultType
        {
            get { return settings["SearchResultType"] ?? "0"; }
        }

        public int SearchResultInCache
        {
            get { return (settings["SearchResultInCache"] == null) ? 1200 : int.Parse(settings["SearchResultInCache"]); }
        }



        #endregion

        #region Avatar Settings

        /// <summary>
        /// Can users upload avatars to their profile.
        /// </summary>

        [ConfigurationProperty("AvatarUpload", IsRequired = false)]
        public bool AvatarUpload
        {

            get
            {
                return (settings["AvatarUpload"] == null) ? true : bool.Parse(settings["AvatarUpload"]);
            }


        }

        /// <summary>
        /// Avatars will be saved within this folder. Provide the path from the web site root. For example Uploads\Avatars.
        /// </summary>
        public string AvatarUploadDir
        {

            get
            {
                return settings["AvatarUploadDir"] ?? "Uploads\\Avatars";
            }


        }


        public string TopicSmiles
        {
          get
          {
              return settings["TopicSmiles"] ?? "/smiles/topics/"; 
          }  
        }




        /// <summary>
        /// Can users use avatars from other websites.
        /// </summary>
        [ConfigurationProperty("AvatarRemote", IsRequired = false)]
        public bool AvatarRemote
        {
            get
            {
                return (settings["AvatarRemote"] == null) ? true : bool.Parse(settings["AvatarRemote"]);
            }

        }

        /// <summary>
        /// Maximum width for avatars.
        /// </summary>
        [ConfigurationProperty("AvatarWidth", IsRequired = false)]
        public int AvatarWidth
        {
            get
            {
                return (settings["AvatarWidth"] == null) ? 120 : int.Parse(settings["AvatarWidth"]);
            }

        }
        /// <summary>
        /// Maximum height for avatars.
        /// </summary>

        [ConfigurationProperty("AvatarHeight", IsRequired = false)]
        public int AvatarHeight
        {
            get
            {
                return (settings["AvatarHeight"] == null) ? 130 : int.Parse(settings["AvatarHeight"]);
            }



        }
        /// <summary>
        /// Maximum size for avatars in kb.
        /// </summary>
        public int AvatarSize
        {
            get
            {
                return (settings["AvatarSize"] == null) ? 50000 : int.Parse(settings["AvatarSize"]);
            }

        }

        #endregion


        #region Permission

        /// <summary>
        /// Should users be able to choose what theme they want to use?
        /// </summary>

        public bool AllowUserTheme
        {
            get { return (settings["AllowUserTheme"] == null) ? false : bool.Parse(settings["AllowUserTheme"]); }

        }
        /// <summary>
        /// Should users be able to choose what language they want to use?
        /// </summary>
        public bool AllowUserLanguage
        {
            get { return (settings["AllowUserLanguage"] == null) ? false : bool.Parse(settings["AllowUserLanguage"]); }


        }



        /// <summary>
        /// Allow users to create signatures.
        /// </summary>
        public bool AllowSignatures
        {
            get { return (settings["AllowSignatures"] == null) ? true : bool.Parse(settings["AllowSignatures"]); }

        }


        /// <summary>
        /// New users won't be able to register.
        /// </summary>
        public bool DisableRegistrations
        {
            get { return (settings["DisableRegistrations"] == null) ? false : bool.Parse(settings["DisableRegistrations"]); }

        }



        public bool AllowGuestReadForums
        {
            get { return (settings["AllowGuestReadForums"] == null) ? false : bool.Parse(settings["AllowGuestReadForums"]); }

        }

        public bool AllowGuestReadTopics
        {
            get { return (settings["AllowGuestReadTopics"] == null) ? false : bool.Parse(settings["AllowGuestReadTopics"]); }

        }

        public bool AllowGuestDownload
        {
            get { return (settings["AllowGuestDownload"] == null) ? false : bool.Parse(settings["AllowGuestDownload"]); }

        }

        public int AntiDdosLevel
        {
            get { return (settings["AntiDdosLevel"] == null) ? 0 : int.Parse(settings["AntiDdosLevel"]); }
        }

        #endregion


        #region EMail Settings

        /// <summary>
        /// Allow users to send emails to each other.
        /// </summary>

        public bool AllowEmailSending
        {
            get { return (settings["AllowEmailSending"] == null) ? true : bool.Parse(settings["AllowEmailSending"]); }

        }


        public int EmailFormat
        {

            get { return settings["EmailFormat"] == null ? 0 : int.Parse(settings["EmailFormat"]); }
        }

        public int EmailsPerCycle
        {

            get { return int.Parse(settings["EmailsPerCycle"]); }
        }

        public int EmailSendAttempts
        {

            get { return int.Parse(settings["EmailSendAttempts"]); }
        }






        /// <summary>
        /// To be able to send posts you need to enter the name of a valid smtp server.
        /// </summary>
        [ConfigurationProperty("SmtpServer", IsRequired = true)]
        public string SmtpServer
        {
            get
            {
                return settings["SmtpServer"];
            }

        }



        /// <summary>
        /// If you need to be authorized to send email.
        /// </summary>
        [ConfigurationProperty("SmtpUserName", IsRequired = true)]
        public string SmtpUserName
        {
            get
            {
                return settings["SmtpUserName"];
            }

        }
        /// <summary>
        /// If you need to be authorized to send email.
        /// </summary>
        [ConfigurationProperty("SmtpUserPass", IsRequired = true)]
        public string SmtpUserPass
        {
            get
            {
                return settings["SmtpUserPass"];
            }


        }


        [ConfigurationProperty("SmtpFrom", IsRequired = false)]
        public string SmtpFrom
        {
            get
            {
                return settings["SmtpFrom"];
            }


        }

        [ConfigurationProperty("SmtpReplyTo", IsRequired = false)]
        public string SmtpReplyTo
        {
            get
            {
                return settings["SmtpReplyTo"];
            }


        }


        public int SmtpPort
        {
            get
            {
                return settings["SmtpPort"] == null ? 25 : int.Parse(settings["SmtpPort"]);
            }


        }

        public bool SmtpSsl
        {
            get
            {
                return settings["SmtpSsl"] == null ? false :Convert.ToBoolean(settings["SmtpSsl"]);
            }


        }


        #endregion


        #region Private Message Options

        /// <summary>
        /// Allow users to access and send private messages.
        /// </summary>
        public bool AllowPrivateMessages
        {
            get { return (settings["AllowPrivateMessages"] == null) ? true : bool.Parse(settings["AllowPrivateMessages"]); }

        }

        public bool AllowPMNotifications
        {
            get
            {
                return bool.Parse(settings["AllowPMNotifications"]);
            }

        }



        public int MaxPrivateMessages
        {
            get
            {
                return int.Parse(settings["MaxPrivateMessages"]);
            }

        }

        public int MaxPrivateMessageRecipients
        {
            get
            {
                return int.Parse(settings["MaxPrivateMessageRecipients"]);
            }

        }


        #endregion



        [ConfigurationProperty("CookieDays", IsRequired = false)]
        public int CookieDays
        {
            get { return (settings["CookieDays"] == null) ? 0 : int.Parse(settings["CookieDays"]); }

        }

        [ConfigurationProperty("RssFetchItems", IsRequired = false)]
        public int RssFetchItems
        {
            get { return (settings["RssFetchItems"] == null) ? 25 : int.Parse(settings["RssFetchItems"]); }

        }



        [ConfigurationProperty("HtmlWhiteTags", IsRequired = false)]
        public string HtmlWhiteTags
        {
            get
            {
                return settings["HtmlWhiteTags"] != null
                           ? settings["HtmlWhiteTags"].Replace(" ", string.Empty)
                           : string.Empty;}

        }


        [ConfigurationProperty("HtmlWhiteAttributes", IsRequired = false)]
        public string HtmlWhiteAttributes
        {
            get
            {
                return settings["HtmlWhiteAttributes"] != null
                           ? settings["HtmlWhiteAttributes"].Replace(" ", string.Empty)
                           : string.Empty;
            }

        }





        



    }
}
